package com.jcxy.controller;

import com.jcxy.mapper.DictionaryMapper;
import com.jcxy.pojo.Dictionary;
import com.jcxy.service.DictionaryService;
import com.jcxy.util.PageBean;
import com.jcxy.util.ParamsDto;
import com.jcxy.util.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/typeManage")
public class TypeManageController {

    @Autowired
    private DictionaryService dictionaryService;
    @Autowired
    private DictionaryMapper dictionaryMapper;
    /**
     * 通过分页获得用户列表
     * @return
     */
    @RequestMapping("/getTypeListByPage.do")
    @ResponseBody
    public ResponseData getTypeListByPage(HttpServletRequest request){


        //将参数转换成合适的map集合
        Map dto = ParamsDto.getDto(request.getParameterMap());

        //调用业务逻辑层方法，查询角色列表
        PageBean pageBean = dictionaryService.queryListByPage(
                dto,//查询条件
                Integer.valueOf(dto.get("page").toString()),//当前页
                Integer.valueOf(dto.get("limit").toString()));//页容量

        return pageBean.getResponseData();
    }

    /**
     * 跳转到typeEdit.jsp页面
     * @param request
     * @return
     */
    @RequestMapping("/toTypeEditPage.do")
    public String toTypeEditPage(HttpServletRequest request){

        Map dto = ParamsDto.getDto(request.getParameterMap());
        //获取用户信息
        List<Dictionary> dictionaryList = dictionaryMapper.queryDictionaryByPage(dto);

        //将用户对象存储在请求中，用于前端显示
        request.setAttribute("dictionary",dictionaryList.get(0));
        //设置页面类型：1-修改页面
        request.setAttribute("pageType",1);

        return "systemManage/typeEdit";
    }

    /**
     * 修改数据字典
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("/updateDictionaryById.do")
    public String updateDictionaryById(HttpServletRequest request) throws Exception{

        Map dto = ParamsDto.getDto(request.getParameterMap());

        dictionaryService.updateDictionary(dto);

        //将操作成功码存放在请求中，用于前端显示提示框
        request.setAttribute("editOption","200");

        return "systemManage/typeManage";
    }

    /**
     * 添加数据字典
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("/addDictionary.do")
    public String addDictionary(HttpServletRequest request) throws Exception{

        Map dto = ParamsDto.getDto(request.getParameterMap());

        dictionaryService.insertDictionary(dto);

        //将操作成功码存放在请求中，用于前端显示提示框
        request.setAttribute("editOption","200");

        return "systemManage/typeManage";
    }

    /**
     * 通过字典id删除字典
     * @param dictionary_id
     * @return
     * @throws Exception
     */
    @RequestMapping("/deleteDictionaryById.do")
    @ResponseBody
    public ResponseData deleteDictionaryById(Integer dictionary_id)throws Exception{

        dictionaryService.deleteDictionaryById(dictionary_id);

        return ResponseData.ok();
    }

}
